package org.loy.eclipse.post_debug_perspective.preferences;

import org.eclipse.jface.preference.*;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.*;

import org.loy.eclipse.post_debug_perspective.Activator;

/**
 * This class represents a preference page that
 * is contributed to the Preferences dialog. By 
 * subclassing <samp>FieldEditorPreferencePage</samp>, we
 * can use the field support built into JFace that allows
 * us to create a page that is small and knows how to 
 * save, restore and apply itself.
 * <p>
 * This page is used to modify preferences only. They
 * are stored in the preference store that belongs to
 * the main plug-in class. That way, preferences can
 * be accessed directly via the preference store.
 */

public class PreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage
{
	public PreferencePage()
	{
		super(GRID);
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
		setDescription("A demonstration of a preference page implementation");
	}

	public void init(IWorkbench workbench)
	{
		workbenchInstance = workbench;
	}
	
	public void createFieldEditors()
	{
		addField( new BooleanFieldEditor( PreferenceConstants.P_IS_ENABLED,	"Switch perspective after debug session", 
			getFieldEditorParent()) );

		// Extract available perspectives and populate combobox 
		IPerspectiveDescriptor[] perspectives = workbenchInstance.getPerspectiveRegistry().getPerspectives();
		
		String[][] entryNamesAndValues = new String[perspectives.length + 1][];

		String[] noneEntry = {"None", PreferenceConstants.DO_NOT_SWITCH_PERSPECTIVE};
		entryNamesAndValues[0] = noneEntry;
		for(int i = 0; i < perspectives.length; i++) {
			String[] entry = {perspectives[i].getLabel(), perspectives[i].getId()};
			entryNamesAndValues[i + 1] = entry;
		}
		
		addField( new ComboFieldEditor(PreferenceConstants.P_PERSPECTIVE_TO_SET, "Perspective to set",
			entryNamesAndValues, getFieldEditorParent()) );
	}

	private IWorkbench workbenchInstance;
}